package com.courier.controller;
import com.courier.entity.Package;
import com.courier.service.PackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/package")
public class PackageController {

    @Autowired
    private PackageService packageService;

    // 新增快递
    @PostMapping
    public boolean addPackage(@RequestBody Package pkg) {
        return packageService.inStorage(pkg);
    }

    // 更新快递信息
    @PutMapping
    public boolean updatePackage(@RequestBody Package pkg) {
        return packageService.updateById(pkg);
    }

    // 删除快递
    @DeleteMapping("/{id}")
    public boolean deletePackage(@PathVariable Long id) {
        return packageService.removeById(id);
    }

    // 入库操作
    @PostMapping("/in/{id}")
    public boolean inStorage(@PathVariable Long id) {
        Package pkg = packageService.getById(id);
        if (pkg != null) {
            return packageService.inStorage(pkg);
        }
        return false;
    }

    // 出库操作
    @PostMapping("/out/{id}")
    public boolean outStorage(@PathVariable Long id) {
        return packageService.outStorage(id);
    }

    // 签收操作
    @PostMapping("/sign/{id}")
    public boolean signFor(@PathVariable Long id) {
        return packageService.signFor(id);
    }
    
    // 带签收人信息的签收操作
    @PostMapping("/sign/{id}/withPerson")
    public boolean signForWithPerson(@PathVariable Long id, @RequestBody SignRequest request) {
        return packageService.signFor(id, request.getSignPerson());
    }
    
    // 用于接收签收请求的内部类
    static class SignRequest {
        private String signPerson;
        
        public String getSignPerson() {
            return signPerson;
        }
        
        public void setSignPerson(String signPerson) {
            this.signPerson = signPerson;
        }
    }

    // 根据ID查询
    @GetMapping("/{id}")
    public Package getById(@PathVariable Long id) {
        return packageService.getById(id);
    }

    // 根据状态查询
    @GetMapping("/status/{status}")
    public List<Package> getByStatus(@PathVariable String status) {
        return packageService.findByStatus(status);
    }

    // 根据快递单号查询
    @GetMapping("/tracking/{trackingNumber}")
    public List<Package> getByTrackingNumber(@PathVariable String trackingNumber) {
        return packageService.findByTrackingNumber(trackingNumber);
    }

    // 分页查询
    @GetMapping("/page")
    public List<Package> getPage(@RequestParam(defaultValue = "1") int page, 
                                 @RequestParam(defaultValue = "10") int pageSize) {
        return packageService.findPage(page, pageSize);
    }

    // 查询所有
    @GetMapping
    public List<Package> getAll() {
        return packageService.list();
    }
}